﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.Properties;

namespace Workflows.Components
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class ForLoopComponent:WorkflowComponentBase
    {
        #region Overrides of WorkflowComponentBase

        private ForLoopInstruction _Instruction;
        /// <summary>an object that encapsulate instruction settings.</summary>
        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction =(ForLoopInstruction)value; }
        }

        private ForLoopOutput _Output;
        /// <summary>an object to encapsulate output</summary>
        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output = (ForLoopOutput) value; }
        }

        /// <summary>
        /// var name
        /// var type
        /// var init
        /// var evaluate
        /// var increment
        /// </summary>
        public ForStatement ForExpression
        {
            get
            {
                return this._Instruction.ForExpression;
            }
            set
            {
                this._Instruction.ForExpression = value;
            }
        }

        /// <summary></summary>
        public Dictionary<string, GlobalVariable> Vars
        {
            get
            {
                return this._Instruction.Vars;
            }
            set
            {
                this._Instruction.Vars = value;
            }
        }

        /// <summary></summary>
        public string InitCodeBlock
        {
            get
            {
                return this._Instruction.InitCodeBlock;
            }
            set
            {
                this._Instruction.InitCodeBlock = value;
            }
        }

        /// <summary></summary>
        public string ConsoleAppFilePath
        {
            get
            {
                return this._Instruction.ConsoleAppFilePath;
            }
            set
            {
                this._Instruction.ConsoleAppFilePath = value;
            }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public ForLoopComponent():base()
        {
            this.ComponentName = "For Loop";
            this.Description = "Loop for specified times and execute enclosed workflow activities each loop";
            this.ComponentIcon = Resources.Compression;
            this.ComponentImage = Resources.Compression48;
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.Misc.ToString();

            this._Instruction=new ForLoopInstruction(
                this.ComponentID, Guid.Empty, new ForStatement(1), 
                new Dictionary<string, GlobalVariable>(), string.Empty, 
                string.Empty, new Dictionary<string, string>());
            this._Output=new ForLoopOutput(this.ComponentID,Guid.Empty,new Dictionary<string, GlobalVariable>());
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);
            this.Instruction.Serialize(ref xDoc, ref xNode);
            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            ForLoopComponent component = (ForLoopComponent)base.Instantiate(xNode);
            component.Instruction = component.Instruction.Instantiate(xNode.SelectSingleNode("Instruction"));
            return component;
        }
    }
}
